package cn.zhage.aimin.canal.client.handler.impl;

import cn.zhage.aimin.canal.client.handler.EntityHandler;
import cn.zhage.aimin.canal.client.handler.RowDataHandler;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.core.ResolvableType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 
 * @author 渣哥
*/
public class RowDataHandlerImpl implements RowDataHandler<CanalEntry.RowData> {

    @Override
    public <T> void handle(CanalEntry.RowData rowData, CanalEntry.EventType eventType, EntityHandler<T> entityHandler) {
        switch (eventType){
            case INSERT:
                List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
                T t = buildEntity(entityHandler, afterColumnsList);
                entityHandler.insert(t);
                break;
            case UPDATE:
                List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
                List<CanalEntry.Column> afterColumnsList1 = rowData.getAfterColumnsList();
                T t1 = buildEntity(entityHandler, beforeColumnsList);
                T t2 = buildEntity(entityHandler, afterColumnsList1);
                entityHandler.update(t1,t2);
                break;
            case DELETE:
                List<CanalEntry.Column> before = rowData.getBeforeColumnsList();
                T t3 = buildEntity(entityHandler, before);
                entityHandler.delete(t3);
                break;
        }
    }
}
